<template>
	<view class="payment-page">

		<view v-if="!orderdetail && !loading" class="empty-cart">
			<u-empty iconSize="200"></u-empty>
		</view>
		<view class="content" v-if="orderdetail">
			<!--地址-->
			<view class="payment-method card">
				<u-cell-group :border="false" v-if="orderdetail.addressinfo">
					<u-cell v-if="orderdetail.addressinfo.id!==-1" :border="false"
						:title="orderdetail.addressinfo.address"
						:label="'收货人:'+orderdetail.addressinfo.name+' 电话:'+orderdetail.addressinfo.tel" :isLink="true"
						icon="/static/icon/收货地址.png" size="large" url="/pages/addressinfo/addressselect"></u-cell>
					<u-cell v-if="orderdetail.addressinfo.id===-1" :border="false" title="请选择收货地址" :isLink="true"
						icon="/static/icon/收货地址.png" size="large" url="/pages/addressinfo/addressselect"></u-cell>
				</u-cell-group>
			</view>
			<view class="payment-method card" v-if="orderdetail.goodslist">
				<!-- 商品信息 -->
				<view class="product-item" v-for="item in orderdetail.goodslist" :key="item.id">
					<image class="product-image" :src="item.picture"></image>
					<view class="product-info">
						<text class="product-name">{{item.name}}</text>
						<text class="product-spec">{{item.color}}</text>
						<text class="product-spec">{{item.size}}</text>
						<text class="product-spec">数量：{{item.qty}}</text>
					</view>
					<text class="product-quantity">￥{{item.price}}</text>
				</view>
			</view>

			<view class="payment-method card" v-if="orderdetail.Deliveryinfo">
				<view class="service-section">
					<view class="section-header">
						<text class="section-title">配送</text>
					</view>
					<view class="service-content">
						<view class="service-item" v-for="item in orderdetail.Deliveryinfo">
							<u-icon name="checkmark-circle" color="#07C160" size="16"></u-icon>
							<text class="service-text">{{item}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="payment-method card" v-if="orderdetail.AfterSales">
				<!-- 售后服务 -->
				<view class="service-section">
					<view class="section-header">
						<text class="section-title">售后</text>
					</view>
					<view class="service-content">
						<view class="service-item" v-for="item in orderdetail.AfterSales">
							<u-icon name="checkmark-circle" color="#07C160" size="16"></u-icon>
							<text class="service-text">{{item}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 订单信息 -->
			<view class="order-info card">
				<view class="info-item">
					<text class="label">商品金额</text>
					<text class="value">¥{{ orderdetail.amount }}</text>
				</view>
				<view class="info-item">
					<text class="label">优惠</text>
					<text class="value">¥{{ orderdetail.distcount }}</text>
				</view>
				<view class="info-item">
					<text class="label">运费</text>
					<text class="value">¥{{ orderdetail.shippingFee }}</text>
				</view>
				<view class="info-item total">
					<text class="label">实付金额</text>
					<text class="value price">¥{{ orderdetail.totalAmount }}</text>
				</view>
			</view>
			<!-- 底部支付按钮 -->
			<view class="footer">
				<u-button type="primary" shape="circle" @click="handlePayment" :loading="loading">
					立即支付 ¥{{ orderdetail.totalAmount }}
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		httpsend
	} from '@/api/api';
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				orderid: null,
			}
		},
		computed: {
			...mapState(['orderinfo'])
		},
		onLoad(options) {
			this.orderid = options.id;
			this.getdata();
		},
		data() {
			return {
				orderdetail: null, //商品订单信息
				paymentMethod: 'wechat', //支付方式
				loading: false //支付加载
			}
		},
		onShow() {
			if (this.orderinfo) {
				//从地址选择页面返回的，重新赋值地址信息
				if (this.orderinfo['type'] === '地址') {
					this.orderdetail.addressinfo['address'] = this.orderinfo['address'];
					this.orderdetail.addressinfo['tel'] = this.orderinfo['tel'];
					this.orderdetail.addressinfo['name'] = this.orderinfo['name'];
					this.orderdetail.addressinfo['id'] = this.orderinfo['id'];
					this.setorderinfo(null);
				}
			}
		},
		methods: {
			...mapMutations(['setorderinfo']),
			async getdata() {
				try {
					uni.showLoading({
						title: '加载中...'
					});
					let param={
						usrid:uni.getStorageSync('usrid'),
						id:this.orderid,
					}
					this.loading = true;
					const res = await httpsend('getgoodsorder');
					if (res?.code === 1) {
						//商品订单信息
						this.orderdetail = res.data;
					} else {
						this.$api.msg(res.msg);
					}
					this.loading = false;
					uni.hideLoading();
				} catch (err) {
					this.loading = false;
					uni.hideLoading();
					console.log('err', err);
				}
			},
			handlePayment() {
				if (this.orderdetail.addressinfo['id'] === -1) {
					this.$api.msg('请先选择收货地址');
					return;
				}
				// 模拟支付流程
				this.loading = true
				this.requestWechatPayment()
			},
			requestWechatPayment() {
				// 微信小程序支付
				// uni.requestPayment({
				// 	provider: 'wxpay',
				// 	timeStamp: String(Date.now()),
				// 	nonceStr: this.generateNonceStr(),
				// 	package: 'prepay_id=wx' + this.generateNonceStr(),
				// 	signType: 'MD5',
				// 	paySign: this.generateNonceStr(),
				// 	success: (res) => {
				// 		uni.showToast({
				// 			title: '支付成功',
				// 			icon: 'success'
				// 		})
				// 		setTimeout(() => {
				// 			this.navigateToOrderDetail()
				// 		}, 1500)
				// 	},
				// 	fail: (err) => {
				// 		uni.showToast({
				// 			title: '支付失败',
				// 			icon: 'error'
				// 		})
				// 	}
				// })
				this.$api.msg('支付成功!');
				this.loading = false;
				//支付完成跳转到代发货页面
				this.navigateToOrderDetail()
			},
			navigateToOrderDetail() {
				uni.navigateTo({
					url: `/pages/order/orderlist?tab=1`
				});
			},

			generateNonceStr() {
				return Math.random().toString(36).substr(2, 15)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.payment-page {
		min-height: 100vh;
		background-color: #f7f7f7;

		.content {
			padding: 20rpx;
			padding-bottom: 120rpx;
		}

		.card {
			background-color: #fff;
			border-radius: 16rpx;
			padding: 24rpx;
			margin-bottom: 20rpx;
		}

		.order-info {
			.info-item {
				display: flex;
				justify-content: space-between;
				margin-bottom: 20rpx;
				font-size: 28rpx;
				color: #666;

				&.total {
					margin-top: 30rpx;
					padding-top: 20rpx;
					border-top: 1rpx solid #eee;

					.label {
						font-weight: bold;
						color: #333;
					}

					.price {
						color: #fa436a;
						font-size: 36rpx;
						font-weight: bold;
					}
				}
			}
		}

		.payment-method {
			.title {
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 30rpx;
				color: #333;
			}

			.method-item {
				padding: 20rpx 0;
				border-bottom: 1rpx solid #f5f5f5;
				display: flex;
				align-items: center;

				.pay-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 20rpx;
				}
			}
		}

		.footer {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 20rpx;
			background-color: #fff;
			box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);

			::v-deep .u-button {
				height: 90rpx;
				font-size: 32rpx;
			}
		}

		.product-item {
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #f5f5f5;

			.product-image {
				width: 180rpx;
				height: 180rpx;
				margin-right: 20rpx;
			}

			.product-info {
				flex: 1;

				.product-name {
					font-size: 28rpx;
					color: #333;
					display: block;
					margin-bottom: 10rpx;
				}

				.product-spec {
					font-size: 24rpx;
					color: #999;
					margin-right: 20rpx;
				}
			}

			.product-quantity {
				font-size: 28rpx;
				color: #666;
			}
		}

		.section-header {
			padding: 20rpx 0;
			border-bottom: 1rpx solid #f5f5f5;

			.section-title {
				font-size: 28rpx;
				color: #333;
				font-weight: bold;
			}
		}

		.delivery-content {
			padding: 20rpx 0;

			.delivery-company {
				font-size: 28rpx;
				color: #333;
				display: block;
				margin-bottom: 10rpx;
			}

			.delivery-time {
				font-size: 28rpx;
				color: #e93b3d;
			}
		}

		.service-content {
			padding: 20rpx 0;

			.service-item {
				display: flex;
				align-items: center;
				margin-bottom: 15rpx;

				.service-text {
					font-size: 26rpx;
					color: #666;
					margin-left: 10rpx;
				}
			}

		}

	}
</style>